/**
 * Created by Administrator on 2016/4/21.
 */

$(function () {
    var language = navigator.language;
    // var language='en';
    if (/zh/.test(language)) {
        $("#container>img.logo").attr("src", "./imgs/logov2.png");
        $("#nav img[alt='SuperMap']").attr("src", "./imgs/logov2.png");
        language = "zh";
    } else {
        $("#container>img.logo").attr("src", "./imgs/logov2en.png");
        $("#nav img[alt='SuperMap']").attr("src", "./imgs/logov2en.png");
        language = "en";
    }
    loadProperties('Messages', 'lang/', language);
    getCurrentLang(language);
    // 顶部导航判断登陆
    if (typeof (UserID) == "undefined" || !UserID || UserID == "null") {
        var protocol=window.location.protocol;
        var hostRefer=window.location.host+"/University/login";
        var opt = "<a href='./login'>" + langThis("ms_login") + "</a> / ";
        opt += "<a href='https://sso.supermap.com/register?service="+protocol+"//"+hostRefer+"' target='_blank'>" + langThis("myinfo_register") + "</a>";
        $("#userRole").html(opt);
    }
    //读写广告
    var item = $("#advertisement>div.carousel-inner[role='listbox']");
    if (item.length) {
        $.getJSON("./lang/advResouces.json", function (data) {
            data = data.data;
            var ol = $("#advertisement>ol.carousel-indicators");
            var listring = "";
            var itemstring = "";
            for (var i in data) {
                listring += '<li data-target="#advertisement" data-slide-to="' + i + '"';
                itemstring += '<div class="item ';
                if (i == 0) {
                    listring += 'class="active"';
                    itemstring += 'active';
                }
                listring += '></li>';
                itemstring += '"><img src="' + data[i].typeurl + '" alt="onlinelab"><div class="carousel-caption"><h3><a href="';
                itemstring += data[i].source + '" target="_blank">';
                itemstring += langThis(data[i].name) + '</a></h3><p>';
                itemstring += langThis(data[i].description) + '</p></div></div>';
            }
            ol.html(listring);
            item.html(itemstring);
        });
    }
    //读写FAQ
    // var faq = $("#supermapfaq>ul");
    // if (faq.length) {
    //     return;
    //     //临时数据
    //     var commonFAQ = source.faq;
    //     var itemsst = "";
    //     for (var i in commonFAQ) {
    //         itemsst += '<li class="listfaq"><span class="listpoint"></span><a title="' + commonFAQ[i].a + '" href="http://support.supermap.com.cn/product/search.aspx?querystr=" target="_blank">' + commonFAQ[i].q + '</a></li>';
    //     }
    //     itemsst += '<li class="seemoreFAQ"><span class="glyphicon glyphicon-chevron-down"></span><a href="http://support.supermap.com.cn/product/search.aspx?querystr=" target="_blank">' + langThis("ms_seemoreFAQ") + '</a></li>';
    //     faq.html(itemsst);
    // }
});
//获取系统语言
function getCurrentLang(e) {
    if (e) {
        this.MyLang = e;
    }
    return this.MyLang;
}
function loadProperties(name, path, lang) {
    var lang = lang || navigator.language;
    jQuery.i18n.properties({
        name: name,
        path: path,
        mode: 'map',
        language: lang,
        callback: function () {
            $("[data-localize]").each(function () {
                var elem = $(this),
                    localizedValue = jQuery.i18n.map[elem.data("localize")];
                if (elem.is("input[type=text]") || elem.is("input[type=password]") || elem.is("input[type=email]") || elem.is("textarea")) {
                    elem.attr("placeholder", localizedValue);
                } else if (elem.is("input[type=button]") || elem.is("input[type=submit]")) {
                    elem.attr("value", localizedValue);
                } else if (elem.is("img")) {
                    elem.attr("title", localizedValue);
                } else {
                    elem.append(localizedValue);
                }
            });
        }
    });
}
function langThis(key) {
    return jQuery.i18n.map[key];
}
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + encodeURI(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
function clearCookie() {
    setCookie("gislab_user", "", -1);
    setCookie("gislab_psw", "", -1);
}
function getCookie(c_name) {
    if (document.cookie.length > 0) {　　//先查询cookie是否为空，为空就return ""
        var c_start = document.cookie.indexOf(c_name + "=")　　//通过String对象的indexOf()来检查这个cookie是否存在，不存在就为 -1　　
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1　　//最后这个+1其实就是表示"="号啦，这样就获取到了cookie值的开始位置
            var c_end = document.cookie.indexOf(";", c_start)　　//其实我刚看见indexOf()第二个参数的时候猛然有点晕，后来想起来表示指定的开始索引的位置...这句是为了得到值的结束位置。因为需要考虑是否是最后一项，所以通过";"号是否存在来判断
            if (c_end == -1) c_end = document.cookie.length
            return decodeURI(document.cookie.substring(c_start, c_end))　　//通过substring()得到了值。

        }
    }

    return ""

}
function getFormartDate() {
    var d = new Date();
    var s = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
    return s;
}